package net.maku.kyy.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhs.trans.service.impl.TransService;
import lombok.AllArgsConstructor;
import net.maku.framework.common.constant.Constant;
import net.maku.framework.common.utils.ExcelUtils;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.kyy.convert.KyyUserGameConvert;
import net.maku.kyy.dao.KyyGameDao;
import net.maku.kyy.dao.KyyUserGameDao;
import net.maku.kyy.entity.KyyUserGameEntity;
import net.maku.kyy.query.KyyUserGameQuery;
import net.maku.kyy.service.KyyUserGameService;
import net.maku.kyy.utils.GetUserMessageUtil;
import net.maku.kyy.vo.KyyGameVO;
import net.maku.kyy.vo.KyyUserGameExcelVO;
import net.maku.kyy.vo.KyyUserGameVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户的游戏表
 *
 * @author MAKU babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@Service
@AllArgsConstructor
public class KyyUserGameServiceImpl extends BaseServiceImpl<KyyUserGameDao, KyyUserGameEntity> implements KyyUserGameService {
    private final TransService transService;

    @Autowired
    private KyyGameDao kyyGameDao;
    @Autowired
    private KyyUserGameDao kyyUserGameDao;

    @Override
    public PageResult<KyyUserGameVO> page(KyyUserGameQuery query) {
        IPage<KyyUserGameEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        return new PageResult<>(KyyUserGameConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }


    private LambdaQueryWrapper<KyyUserGameEntity> getWrapper(KyyUserGameQuery query){
        LambdaQueryWrapper<KyyUserGameEntity> wrapper = Wrappers.lambdaQuery();

        return wrapper;
    }


    @Override
    public KyyUserGameVO get(Long id) {
        KyyUserGameEntity entity = baseMapper.selectById(id);
        KyyUserGameVO vo = KyyUserGameConvert.INSTANCE.convert(entity);

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(KyyUserGameVO vo) {
        Long userId = GetUserMessageUtil.getUserId();
        //检查该游戏是否已经存在该用户中
        Boolean isExist = baseMapper.selectOne(new LambdaQueryWrapper<KyyUserGameEntity>().eq(KyyUserGameEntity::getKyyGameId, vo.getKyyGameId()).eq(KyyUserGameEntity::getCreator, userId))!= null;
        if (isExist){
            return;
        }
        KyyUserGameEntity entity = KyyUserGameConvert.INSTANCE.convert(vo);
        entity.setCreator(userId);
        entity.setUpdater(userId);
        entity.setDeleted(Constant.DELETED_NO);//删除状态正常:未删除

        baseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(KyyUserGameVO vo) {
        KyyUserGameEntity entity = KyyUserGameConvert.INSTANCE.convert(vo);

        updateById(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);

    }



    @Override
    public void export() {
    List<KyyUserGameExcelVO> excelList = KyyUserGameConvert.INSTANCE.convertExcelList(list());
        transService.transBatch(excelList);
        ExcelUtils.excelExport(KyyUserGameExcelVO.class, "用户的游戏表", null, excelList);
    }

    /**
     * 获取我的游戏列表
     * @return
     */
    @Override
    public PageResult<KyyUserGameVO> getMyMap() {
        Long userId = GetUserMessageUtil.getUserId();
        //根据userId获取属于我的游戏页面
        IPage<KyyUserGameEntity> page = baseMapper.selectPage(new Page<KyyUserGameEntity>(), new LambdaQueryWrapper<KyyUserGameEntity>().eq(KyyUserGameEntity::getCreator, userId));
        return new PageResult<>(KyyUserGameConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    /**
     * 获取我的游戏详细列表
     * @return
     */
    @Override
    public PageResult<KyyGameVO> getMyGamePage() {
        Long userId = GetUserMessageUtil.getUserId();
        //获取属于的所有游戏id
        List<Long> gameIds = kyyUserGameDao.getGameIdsByUserId(userId);
        if (gameIds.size() == 0){
            return null;
        }
        IPage<KyyGameVO> page = kyyGameDao.getMyGamePageByIds(new Page<KyyGameVO>(), gameIds);
        PageResult<KyyGameVO> pageResult = new PageResult<>(page.getRecords(), page.getTotal());
        return pageResult;
    }
}